/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Visual_familia.java
 *
 * Created on 19/06/2011, 23:39:24
 */
package visuales;

import entidades.Animal;
import entidades.Bateria;
import entidades.Camara;
import entidades.DetalleFamilia;
import entidades.Familia;
import entidades.Jaula;
import entidades.TipoCamara;
import entidades.TipoEvento;
import gestores.GestorProduccion;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Facundo
 */
public class Visual_familia extends javax.swing.JFrame {

    /** Creates new form Visual_familia */
    private JFrame pantalla;
    private GestorProduccion gp;
    private DefaultTableModel dtm;
    private ArrayList ubicacionesFamilia;
    private int cant_click;

    public Visual_familia(JFrame padre) {
        initComponents();
        gp = new GestorProduccion();
        this.pantalla = padre;
        dtm = new DefaultTableModel();
        cant_click = 0;
        this.armarTabla();
        if (this.buscarNuevaUbicacion()) {
            this.cargarComboMachos();
            txtFechaConformacion.setText(String.valueOf(this.gp.tomarFechaSistema().dateValue()));
        } else {
            return;
        }

    }

    public ArrayList getUbicacionesFamilia() {
        return ubicacionesFamilia;
    }

    public void setUbicacionesFamilia(ArrayList ubicacionesFamilia) {
        this.ubicacionesFamilia = ubicacionesFamilia;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        btnMostrarDatosMachoFamilia = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtMadre = new javax.swing.JTextField();
        txtPadre = new javax.swing.JTextField();
        txtUbicacion = new javax.swing.JTextField();
        jcbMachoFamilia = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        btnAgregarHembra = new javax.swing.JButton();
        jcbHembraFamilia = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        grillaFamilia = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtFechaConformacion = new javax.swing.JFormattedTextField();
        btnConfirmarFamilia = new javax.swing.JButton();
        btnCancelarFamilia = new javax.swing.JButton();
        btnImprimirTarjetaFamilia = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("PampaChin- Familia");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Ingresar Macho", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel2.setText("Macho reproductor:");

        btnMostrarDatosMachoFamilia.setText("Mostrar datos");
        btnMostrarDatosMachoFamilia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMostrarDatosMachoFamiliaActionPerformed(evt);
            }
        });

        jLabel3.setText("Madre:");

        jLabel4.setText("Padre:");

        jLabel5.setText("Ubicacion:");

        txtMadre.setEditable(false);

        txtPadre.setEditable(false);

        txtUbicacion.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jcbMachoFamilia, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnMostrarDatosMachoFamilia))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, 505, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(txtMadre, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(txtPadre)))))
                .addGap(40, 40, 40))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(btnMostrarDatosMachoFamilia)
                    .addComponent(jcbMachoFamilia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Ingresar Hembras", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel6.setText("Ingrese el codigo de una hembra:");

        btnAgregarHembra.setText("Agregar");
        btnAgregarHembra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarHembraActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbHembraFamilia, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAgregarHembra)
                .addContainerGap(170, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(btnAgregarHembra)
                    .addComponent(jcbHembraFamilia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Hembras integrantes de la familia", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        grillaFamilia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(grillaFamilia);

        jLabel1.setText("Fecha de Conformacion :");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtFechaConformacion, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFechaConformacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnConfirmarFamilia.setText("Confirmar");
        btnConfirmarFamilia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarFamiliaActionPerformed(evt);
            }
        });

        btnCancelarFamilia.setText("Cancelar");
        btnCancelarFamilia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarFamiliaActionPerformed(evt);
            }
        });

        btnImprimirTarjetaFamilia.setText("Imprimir Tarjetas");
        btnImprimirTarjetaFamilia.setEnabled(false);
        btnImprimirTarjetaFamilia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirTarjetaFamiliaActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnConfirmarFamilia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelarFamilia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnImprimirTarjetaFamilia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 284, Short.MAX_VALUE)
                        .addComponent(btnSalir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConfirmarFamilia)
                    .addComponent(btnCancelarFamilia)
                    .addComponent(btnSalir)
                    .addComponent(btnImprimirTarjetaFamilia, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnMostrarDatosMachoFamiliaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMostrarDatosMachoFamiliaActionPerformed
        this.mostrarDatosMacho((Animal) jcbMachoFamilia.getSelectedItem());
        //boton para encontrar un macho reproductor para armar una familia
        /*
        try {
        String nombre = new String(jTextField1.getText());
        ani = ga.getAnimal(nombre);
        if (ani != null) {
        jTextField2.setText(ani.getMadre());
        jTextField3.setText(ani.getPadre());
        jTextField4.setText(String.valueOf(ani.getUbicacion()));
        jButton4.setEnabled(false);
        jTextField1.setEditable(false);
        //this.habiltarCampos(false);//cambiar a true si hacemos el update de stock
        //btnBaja.setEnabled(true);
        //jTextField5.setEditable(true);

        } else {
        JOptionPane.showMessageDialog(null, "El nombre de animal ingresado es icorrecto", "Advertencia", JOptionPane.ERROR_MESSAGE);
        }
        } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Error ocurrido!!", "Atencion", JOptionPane.ERROR_MESSAGE);
        return;
        }
         *
         */
}//GEN-LAST:event_btnMostrarDatosMachoFamiliaActionPerformed

    private void btnAgregarHembraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarHembraActionPerformed
        if (cant_click != 6) {
            this.agregarHembraFamilia((Animal) jcbHembraFamilia.getSelectedItem(), cant_click);
            cant_click++;
        } else {
            this.mensajeError(btnAgregarHembra, "Cantidad de hembras completas para esa familia.");
        }
        //boton para encontrar una hembra reproductor para armar una familia
        /*
        try {
        String nombre = new String(jTextField5.getText());
        ani = ga.getAnimal(nombre);
        if (ani != null) {

        //se agrega la hembra a la grilla!

        } else {
        JOptionPane.showMessageDialog(null, "El nombre de animal ingresado es icorrecto", "Advertencia", JOptionPane.ERROR_MESSAGE);
        }
        } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Error ocurrido!!", "Atencion", JOptionPane.ERROR_MESSAGE);
        return;
        }
         *
         */
}//GEN-LAST:event_btnAgregarHembraActionPerformed

    private void btnConfirmarFamiliaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarFamiliaActionPerformed
        this.tomarConfirmacion(true);

        /*
         * luego de que en la grilla figure la familia armada correctamente
         * se registra en la base de datos la familia conformada!!!
         */
    }//GEN-LAST:event_btnConfirmarFamiliaActionPerformed

    private void btnCancelarFamiliaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarFamiliaActionPerformed
        this.tomarConfirmacion(false);
        /* cuando se presiona el boton cancelar
         * se habilita el jtextField1
         * se habilita el jtextField5
         * se habilita el boton Agregar Macho jbutton4
         * se limpian todos los campos
         * se borra la grilla
         */
        /*
        jTextField1.setEditable(true);
        jTextField5.setEditable(true);
        jButton4.setEnabled(true);
        this.borrarCampos();

        //borrar grilla!!!
         *
         */
    }//GEN-LAST:event_btnCancelarFamiliaActionPerformed

    private void btnImprimirTarjetaFamiliaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirTarjetaFamiliaActionPerformed
        /*
         * boton que permite imprimir las tarjetas de familias armadas!!
         */
    }//GEN-LAST:event_btnImprimirTarjetaFamiliaActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        //Salimos de la ventana registrar_familia
        this.dispose();
        pantalla.setVisible(true);
        //padre.setVisible(true);
}//GEN-LAST:event_btnSalirActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new Visual_familia().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarHembra;
    private javax.swing.JButton btnCancelarFamilia;
    private javax.swing.JButton btnConfirmarFamilia;
    private javax.swing.JButton btnImprimirTarjetaFamilia;
    private javax.swing.JButton btnMostrarDatosMachoFamilia;
    private javax.swing.JButton btnSalir;
    private javax.swing.JTable grillaFamilia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbHembraFamilia;
    private javax.swing.JComboBox jcbMachoFamilia;
    private javax.swing.JFormattedTextField txtFechaConformacion;
    private javax.swing.JTextField txtMadre;
    private javax.swing.JTextField txtPadre;
    private javax.swing.JTextField txtUbicacion;
    // End of variables declaration//GEN-END:variables

    private void cargarComboMachos() {
//        no ralgreado, no pertenezca a alguna familia, edad < 8 meses
        Animal res_animalMacho;
        Animal res_animalHembra;
        ArrayList res_machos = gp.buscarMachos();
        if (res_machos != null && res_machos.isEmpty() == false) {
            Iterator iter_res_machos = res_machos.iterator();
            while (iter_res_machos.hasNext()) {
                res_animalMacho = (Animal) iter_res_machos.next();
                jcbMachoFamilia.addItem(res_animalMacho);
            }
            ArrayList res_hembras = gp.buscarHembras();
            if (res_hembras != null && res_hembras.isEmpty() == false) {
                Iterator iter_res_hembras = res_hembras.iterator();
                while (iter_res_hembras.hasNext()) {
                    res_animalHembra = (Animal) iter_res_hembras.next();
                    jcbHembraFamilia.addItem(res_animalHembra);
                }
            } else {
                this.mensajeError(btnAgregarHembra, null);
                this.mensajeError(btnMostrarDatosMachoFamilia, null);
                this.mensajeError(btnConfirmarFamilia, "No hay hembras disponibles para formar una familia.");
            }
        } else {
            this.mensajeError(btnMostrarDatosMachoFamilia, null);
            this.mensajeError(btnAgregarHembra, null);
            this.mensajeError(btnConfirmarFamilia, "No hay machos disponibles para formar una familia.");
        }
    }

    private void mostrarDatosMacho(Animal animal) {
        txtMadre.setText(animal.getMadre().getNombre());
        txtPadre.setText(animal.getPadre().getNombre());
        Camara res_camara = animal.getUbicacion();
        TipoCamara res_tipoCamara = res_camara.getTipo_camara();
        Bateria res_bateria = (Bateria) animal.getUbicacion().getBateria().get(0);
        Jaula res_jaula = res_bateria.getJaula().get(0);
        String ubicacion =/* "Camara: " + res_camara.getNombre()
                +*/ " - Tipo de camara: " + res_tipoCamara.getNombre()
                + " - Bateria: " + res_bateria.getNombre()
                + " - Jaula: " + res_jaula.getNombre();
        txtUbicacion.setText(ubicacion);
    }

    private void mensajeError(JButton boton, String msg) {
        if (boton != null) {
            boton.setEnabled(false);
        }
        if (msg != null) {
            System.out.println("Error: " + msg);
            JOptionPane.showMessageDialog(null, msg, "Atencion", JOptionPane.ERROR_MESSAGE);
        }
        return;
    }

    private void agregarHembraFamilia(Animal animal, int pos) {
        //como hago para hacer esto??? nombre, padre, madre, ubicacion
        this.insertarHembra(animal, pos);
    }

    private void armarTabla() {
        dtm.addColumn("Nombre");
        dtm.addColumn("Padre");
        dtm.addColumn("Madre");
        dtm.addColumn("Ubicacion actual en maduracion");
        dtm.addColumn("Nueva ubicacion en reproducion");
        dtm.addColumn("Descripcion de nueva ubicacion");
        grillaFamilia.setModel(dtm);
    }

    private void insertarHembra(Animal animal, int pos) {
//        grillaFamilia.getModel();
        Object fila[] = new Object[6];
        fila[0] = animal;
        fila[1] = animal.getPadre();
        fila[2] = animal.getMadre();

        Bateria res_bateria = (Bateria) animal.getUbicacion().getBateria().get(0);
        Jaula res_jaula = res_bateria.getJaula().get(0);
        String ubicacion_actual =
                //                res_camara.getNombre() + " - " +
                //                res_tipoCamara.getNombre()                +
                //                " - " +
                res_bateria.getNombre() + " - " + res_jaula.getNombre();
        fila[3] = ubicacion_actual;



        Bateria res_bateria_nueva = gp.getBateriaNuevaUbicacion();
        if (res_bateria_nueva == null) {
            System.out.println("aca ta el error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
//        String ubicacion_final = "aca tengo que tener los objetos de la nueva ubicacion...";
        String ubicacion_final = res_bateria_nueva.getNombre() + " - " + this.getUbicacionesFamilia().get(pos);


        Camara camara_nueva = new Camara();
        camara_nueva.setNombre("CAMARA1");
        camara_nueva.setTipo_camara(new TipoCamara("REPRODUCCION", "Reproduccion"));
        ArrayList bateria_nueva = new ArrayList();
        bateria_nueva.add(res_bateria_nueva);
        ArrayList jaula_nueva = new ArrayList();
        jaula_nueva.add(res_bateria_nueva.getJaula().get(pos));
        res_bateria_nueva.setJaula(jaula_nueva);
        camara_nueva.setBateria(bateria_nueva);
        animal.setUbicacion(camara_nueva);


        fila[4] = camara_nueva;
        fila[5] = ubicacion_final;
        dtm.addRow(fila);
        grillaFamilia.setModel(dtm);
        System.out.println("Grande pelusa!");
    }

    private void confirmarFamilia() {
        int cols = dtm.getColumnCount();
        int fils = dtm.getRowCount();
        System.out.println("Cantidad de filas: " + fils + " - columnas: " + cols);
        Animal ani_hembra = null;
        ArrayList res_animalesAsignados = new ArrayList();
        Animal macho_familia = (Animal) jcbMachoFamilia.getSelectedItem();
        boolean res_ok = false;
        for (int i = 0; i < fils; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.println("Columna: " + j);
                if (j == 0) {
                    ani_hembra = (Animal) dtm.getValueAt(i, j);
                }
                if (j == 4) {
                    ani_hembra.setUbicacion((Camara) dtm.getValueAt(i, j));
                    System.out.println("camara: " + ani_hembra.getUbicacion().getNombre() + " tipo de camara: " + ani_hembra.getUbicacion().getTipo_camara().getNombre());
                    Bateria bat_aux = (Bateria) ani_hembra.getUbicacion().getBateria().get(0);
                    System.out.println("Bateria: " + bat_aux.getNombre());
                    Jaula jau_aux = bat_aux.getJaula().get(0);
                    System.out.println("Jaula: " + jau_aux.getNombre());
//                    System.out.println("Ubicacion: " + dtm.getValueAt(i, j));
                    res_ok = true;
                }
                if (res_ok == true) {
                    System.out.println("Nombre de hembra asignada a la familia: " + ani_hembra);
                    if (res_animalesAsignados.contains(ani_hembra)==false) {
                        res_animalesAsignados.add(ani_hembra);
                    }
                    //det=new detalleFamilia();
                    //res_detalle.add(det);
                    res_ok = false;
                }
//                System.out.println(dtm.getValueAt(i, j));
            }
        }
//        if (res_ok==false) {
        Animal primeraHembra = (Animal) res_animalesAsignados.get(0);
//        Animal primeraHembra = (Animal) res_detalle.get(0);
        macho_familia.setUbicacion(primeraHembra.getUbicacion());

//        aca se conforma la familia, esta del armado del detalle de la familia, esta hecho en el res_ok == true

        Iterator iter_resAnimalesAsignados = res_animalesAsignados.iterator();
        DetalleFamilia det_familia = null;
        Animal item_resAnimalesAsignados = null;
        ArrayList res_detalleFamilia = new ArrayList();
        while (iter_resAnimalesAsignados.hasNext()) {
            item_resAnimalesAsignados = (Animal) iter_resAnimalesAsignados.next();
            det_familia = new DetalleFamilia();
            det_familia.setHembra(item_resAnimalesAsignados);
            det_familia.setFecha_desde(this.gp.tomarFechaSistema());
            res_detalleFamilia.add(det_familia);
        }


        Familia res_familia = new Familia();
        res_familia.setFecha_conformacion(this.gp.tomarFechaSistema());
        res_familia.setPadre(macho_familia);
        res_familia.setDetalle(res_detalleFamilia);

        //la linea de abajo es igual que el familia.setPadre(macho_familia)
        res_animalesAsignados.add(macho_familia);


        this.gp.setFamilia(res_familia);
        this.gp.setListaFamilia(res_animalesAsignados);
        //estos 2 metodos hacen la misma cosa


        String msg = this.gp.confirmacionAceptada(true, TipoEvento.ASIGNAR);
        if (msg.compareToIgnoreCase("OK") != 0) {
            this.mensajeError(btnConfirmarFamilia, "Error al formar la familia.");
        } else {
            JOptionPane.showMessageDialog(this, msg);
            this.dispose();
            this.pantalla.setVisible(true);
        }
    }

    private boolean buscarNuevaUbicacion() {
        System.out.println("Buscando las ubicaciones disponibles.");
        ArrayList res_nuevaUbicacion;
        res_nuevaUbicacion = gp.buscarNuevaUbicacionFamilia();
        if (res_nuevaUbicacion == null || res_nuevaUbicacion.isEmpty() == true) {
            this.mensajeError(btnConfirmarFamilia, null);
            this.mensajeError(btnAgregarHembra, null);
            this.mensajeError(btnMostrarDatosMachoFamilia, "No hay ubicaciones disponibles para formar una familia");
            return false;
        } else {
            this.setUbicacionesFamilia(res_nuevaUbicacion);
        }
        System.out.println("Metodo: buscarNuevaUbicacion... aca hay que descomentar para que ande todo bien...");
        return true;
    }

    private void tomarConfirmacion(boolean acepto) {
        if (acepto) {
            this.confirmarFamilia();
        } else {
            String res = gp.confirmacionAceptada(acepto, -1);
            System.out.println("Se cancela las acciones que se estaban realizando: " + res);
            this.dispose();
            this.pantalla.setVisible(true);
        }


    }
}
